﻿using ConfExpert.Datasets;
using DevTeam;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Siparis
{
    public partial class Zegna : XISiparis
    {
        public Zegna()
            : base()
        {
            InitializeComponent();

            //Init("Destex GF Çeki Listesi", true);
            this.FileDone += this_FileDone;
            //this.SplitterWidthPercentage = 30;
        }

        void this_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                DataRowCollection rows = dt.Rows;
                DataColumnCollection cols = dt.Columns;

                if (rows.Count > 1)
                    if (rows[0][0].ToStr() != string.Empty)
                    {
                        //Orders

                        for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                        {
                            DataRow dr0 = dt.Rows[0];
                            string colName = dr0[colIndex].ToStr();
                            switch (colName)
                            {
                                case "NAZIONE": dt.Columns[colIndex].ColumnName = "Ek11"; break;
                                case "UDB": dt.Columns[colIndex].ColumnName = "Ek12"; break;
                                case "CLI": dt.Columns[colIndex].ColumnName = "Ek13"; break;
                                case "STAG": dt.Columns[colIndex].ColumnName = "Sezon"; break;
                                case "ORD": dt.Columns[colIndex].ColumnName = "OrderNr"; break;
                                case "RIGA": dt.Columns[colIndex].ColumnName = "Ek14"; break;
                                case "LANCIO": dt.Columns[colIndex].ColumnName = "Ek15"; break;
                                case "TESS": dt.Columns[colIndex].ColumnName = "Artikel"; break;
                                case "MOD": dt.Columns[colIndex].ColumnName = "Model"; break;
                                case "MADE IN": dt.Columns[colIndex].ColumnName = "Ek16"; break;
                                case "CODICE ETICHETTA": dt.Columns[colIndex].ColumnName = "Ek17"; break;
                                case "N° PAC": dt.Columns[colIndex].ColumnName = "Ek18"; break;
                                case "N° MATRICOLA": dt.Columns[colIndex].ColumnName = "N° MATRICOLA"; break;
                            }
                        }

                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dt.Rows.IndexOf(dr) < 1)
                                continue;

                            MyData.OrdersRow drOrd = Ord.NewOrdersRow();

                            drOrd.Ek11 = dr["Ek11"].ToStr();
                            drOrd.Ek12 = dr["Ek12"].ToStr();
                            drOrd.Ek13 = dr["Ek13"].ToStr();
                            drOrd.Ek14 = dr["Ek14"].ToStr();
                            drOrd.Ek15 = dr["Ek15"].ToStr();
                            drOrd.Ek16 = dr["Ek16"].ToStr();
                            drOrd.Ek17 = //dr["Ek17"].ToStr();
                                dr["Sezon"].ToStr() + " " + dr["Ek15"].ToStr() + " " + dr["N° MATRICOLA"].ToStr();
                            drOrd.Ek18 = dr["Ek18"].ToStr();
                            drOrd.Sezon = dr["Sezon"].ToStr();
                            drOrd.OrderNr = dr["OrderNr"].ToStr();
                            drOrd.Artikel = dr["Artikel"].ToStr();
                            drOrd.Model = dr["Model"].ToStr();
                            Ord.AddOrdersRow(drOrd);
                        }
                    }


                    else
                    {
                        //Magaza Dagilimi

                        int start = 0;
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dr[1].ToStr() != string.Empty)
                            {
                                start = dt.Rows.IndexOf(dr);
                                break;
                            }
                        }

                        int D = 0;
                        for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                        {
                            DataRow dr0 = dt.Rows[start];
                            string colName = dr0[colIndex].ToStr();
                            switch (colName)
                            {
                                case "Ordine": dt.Columns[colIndex].ColumnName = "OrderNr"; break;
                                case "Tessuto": dt.Columns[colIndex].ColumnName = "Artikel"; break;
                                case "Modello": dt.Columns[colIndex].ColumnName = "Model"; break;
                                case "Riga": dt.Columns[colIndex].ColumnName = "Ek14"; break;
                                case "Numero Lancio": dt.Columns[colIndex].ColumnName = "Ek15"; break;

                                case "Cliente": dt.Columns[colIndex].ColumnName = "MagazaKodu"; break;
                                case "Ragione Sociale": dt.Columns[colIndex].ColumnName = "Magaza"; break;
                                case "Totale Capi Lanciati": D = colIndex + 1; break;
                                //DagilimId -> Totale Capi Lanciati sütununun yanındaki sütunun harfine göre??
                            }
                        }

                        Dictionary<string, string[]> asorti = new Dictionary<string, string[]>();
                        for (int rowIndex = 0; rowIndex < start; rowIndex++)
                        {
                            DataRow dr0 = dt.Rows[rowIndex];
                            string colName = dr0[D].ToStr();
                            if (colName != string.Empty)
                            {
                                asorti.Add(colName, new string[64]);

                                for (int colIndex = D + 1; colIndex < dt.Columns.Count; colIndex++)
                                    asorti[colName][colIndex] = dr0[colIndex].ToStr();
                            }
                        }

                        foreach (DataRow dr in dt.Rows)
                        {
                            if (dt.Rows.IndexOf(dr) < start + 1)    //1/2?
                                continue;

                            DataRow drMag = Mag.NewRow();
                            drMag["OrderNr"] = dr["OrderNr"].ToStr();
                            drMag["Artikel"] = dr["Artikel"].ToStr();
                            drMag["Model"] = dr["Model"].ToStr();
                            drMag["Ek14"] = dr["Ek14"].ToStr();
                            drMag["Ek15"] = dr["Ek15"].ToStr();
                            drMag["MagazaKodu"] = dr["MagazaKodu"].ToStr();
                            drMag["Magaza"] = dr["Magaza"].ToStr();

                            int DagilimId = 0;
                            switch (dr[D].ToStr())
                            {
                                case "E": DagilimId = 10; break;
                                case "F": DagilimId = 7; break;
                            }

                            if (DagilimId > 0)
                            {
                                drMag["DagilimId"] = DagilimId;   //Dagilim ve diğer beden sütunlarını ekle!!
                                for (int i = D + 1; i < dt.Columns.Count; i++)
                                    if (dr[i].ToInt() > 0)
                                    {
                                        string beden = asorti[dr[D].ToStr()][i].ToStr();   // dt.Rows[start][i].ToStr();

                                        if (beden == "XXXL")
                                            beden = "3XL";

                                        DataRow[] rx = dtDag.Select("dagilimId=" + DagilimId.ToStr() + " AND Beden='" + beden + "'");
                                        if (rx.Length > 0)
                                        {
                                            string d = rx[0]["Name"].ToStr();
                                            drMag[d] = dr[i];
                                        }
                                    }

                                Mag.Rows.Add(drMag);
                            }
                        }
                    }

            }
        }

    }
}